import fs from 'fs/promises'
import path from 'path'

import testMatrix from './_matrix'

const nodeRuntime = 'runtime/client'
const edgeRuntime = 'runtime/wasm-compiler-edge'

testMatrix.setupTestSuite(
  ({ generatorType }, suiteMeta, clientMeta) => {
    const clientEntrypoint = `generated/prisma/client/index.js`
    const clientEntrypointPath = path.join(suiteMeta.generatedFolder, clientEntrypoint)

    describeIf(generatorType === 'prisma-client-js')('runtime bundles in JS client', () => {
      test('imports correct runtime', async () => {
        const generatedClientContents = await fs.readFile(clientEntrypointPath, 'utf-8')

        switch (clientMeta.runtime) {
          case 'client':
            expect(generatedClientContents).toContain(nodeRuntime)
            expect(generatedClientContents).not.toContain(edgeRuntime)
            break
          case 'wasm-compiler-edge':
            expect(generatedClientContents).not.toContain(nodeRuntime)
            expect(generatedClientContents).toContain(edgeRuntime)
            break
          default:
            clientMeta.runtime satisfies never
        }
      })

      test('imported files have the expected annotations', async () => {
        const generatedClientContents = await fs.readFile(clientEntrypointPath, 'utf-8')

        expect(generatedClientContents).toContain('/* !!! This is code generated by Prisma. Do not edit directly. !!!')
        expect(generatedClientContents).toContain('/* eslint-disable */')
        expect(generatedClientContents).toContain('// biome-ignore-all lint: generated file')
      })
    })
  },
  {
    skipDefaultClientInstance: true,
  },
)
